<?php
namespace Admin\Controller;

class NewsController extends AuthController {

    public function index(){
        $news = M('news')
            ->join('dx_news_category on dx_news.category_id = dx_news_category.id')
            ->field(array('*','dx_news.id'=>'news_id'))
            ->select();
        foreach($news as $key=>$val){

            //$new[$key]['img_url'] = json_decode($val['img_url']);
            $img = M('news')
                ->join('dx_image on dx_news.id = dx_image.news_id')
                ->where('news_id=%d',$val['news_id'])
                ->limit(0,1)
                ->select();
            //dump($img);
            foreach($img as $k=>$v){
                $img[$k]['img_url']=json_decode($v['img_url']);
            }
            $news[$key]['img']=$img;
        }
        $this->assign('news',$news);
        $this->display('news');
    }
    /* 添加新闻 */
    public function add(){
        $users = session('admin_user');
        $category = M('news_category')->select(); //新闻分类
        if(IS_POST){
            $time = date('Y-m-d',time());
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  = 	'./Public';
            $upload->savePath  =      '/admin/images/news/'.$time."/"; // 设置附件上传目录
            $upload->saveName  = '';
            // 上传文件
            $info   =   $upload->upload();
            //dump($info);
            $array = array();
            $new = M('news');
            $_POST['n_ctime'] = time();
            $_POST['admin_user_id'] = $users['id'];
            if($news = $new->add($_POST)){
                if($info){
                    foreach($info as $k=>$val){
                        $array[$k][$val['key']] = json_encode($info[$k]['savepath'].$info[$k]['savename']);
                        $array[$k]['img_type'] = $info[$k]['type'];
                        $array[$k]['news_id'] =$news;
                    }
                }
                $_POST['images'] = $array;
                $img = M('image');
                if($img->addAll($_POST['images'])){
                    $this->success('添加成功' , U('news/index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($news->getError());
            }

            //批量存入图片
            /*if($img_id = $img->addAll($_POST['images'])){
                $idmax = $img_id+count($array);//获取存入图片的最大Id
                $image = M('image')->field('id')->where('id>='.$img_id. ' and id<='.$idmax)->select();//读取出刚存入的图片的id（二维数组）
                $arr = array();
                foreach($image as $kk=>$vv){
                    $arr[] =$vv['id'];//把二维数组转为一维数组
                }
                $arr = implode(",",$arr);//把数组转为字符串
                $_POST['image_id'] = $arr;
                $_POST['n_ctime'] = time();
                $new = M('new');
                if($news = $new->add($_POST)){
                    $this->success('添加成功' , U('news/index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($img->getError());
            }*/
        }
        $this->assign('category' , $category);
        $this->display('add');
    }
    /* 修改新闻 */
    public function edit($id){
        $category = M('news_category')->select(); //新闻分类
        //获取新闻原始值
        $news = M('news')
            ->join('dx_news_category on dx_news.category_id = dx_news_category.id')
            ->field(array('*','dx_news.id'=>'news_id'))
            ->where('dx_news.id ='.$id)
            ->find();
            $img = M('news')
                ->join('dx_image on dx_news.id = dx_image.news_id')
                ->field(array('*','dx_image.id'=>'img_id'))
                ->where('news_id=%d',$id)
                ->select();
            foreach($img as $k=>$v){
                $img[$k]['img_url']=json_decode($v['img_url']);
            }
            dump($img);
        //修改新闻
        if(IS_POST){
            //dump($_POST);exit;
            $new = M('news');
            $time = date('Y-m-d',time());
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  = 	'./Public';
            $upload->savePath  =      '/admin/images/news/'.$time."/"; // 设置附件上传目录
            $upload->saveName  = '';
            // 上传文件
            $info   =   $upload->upload();
            $image = array();
            $new = M('news');
            $_POST['n_ctime'] = time();
            if($news = $new->where('id='.$_POST['id'])->save($_POST)){
                if(!empty($info)){
                    foreach($info as $k=>$val){
                        $image[$k][$val['key']] = json_encode($info[$k]['savepath'].$info[$k]['savename']);
                        $image[$k]['img_type'] = $info[$k]['type'];
                        $image[$k]['news_id'] =$news;
                    }
                }
                $new_img = M('image');
                if($new_img->addAll($image)){
                    $this->success('修改成功' , U('news/index'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error($news->getError());
            }

        }
        $this->assign('img',$img);
        $this->assign('news' , $news);
        $this->assign('category' , $category);
        $this->display('edit');
    }
    /* 添加新闻分类 */
    public function news_category(){
        $category = M('news_category')->select();

        $parent_category = M('news_category')->where('parent_id=0')->select();
        if(IS_POST){
            $new_category = M('news_category');
            if($new_category->add($_POST)){
                $this->success('添加成功' , U('news/news_category'));
            }else{
                $this->error('添加失败');
            }
        }
        $this->assign('category' ,$category);
        $this->assign('parent_category' ,$parent_category);
        $this->display('news_category');
    }
    /* 修改新闻分类  */
    public function ajax(){
        //给修改页面赋原始值
        if(IS_GET){
            $new = M('news_category')->where('id='.$_GET['id'])->find();
            $this->ajaxReturn($new);
        }
        //修改为最新值
        if(IS_POST){
            $news = M('news_category');
            if($news->where('id='.$_POST['id'])->save($_POST)){
                $str =  "<script>$('#alter').css('display','none');</script>";
                echo $str;
                $this->success('修改成功' , U('news/news_category'));

            }else{
                $this->error('修改失败');
            }
        }

    }
    /* 删除新闻分类 */
    public function del(){
        if(IS_GET){
            $new = M('news_category');
            if($new->where('id='.$_GET['id'])->delete()){
                $this->success('删除成功' , U('news/news_category'));
            }else{
                $this->error('删除失败');
            }
            $this->ajaxReturn();
        }

    }
}